﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;

public partial class CustomLogin : System.Web.UI.Page
{
    BookStoreDataContext bs = new BookStoreDataContext();
    string connString = "server=HOME; database=bookStore; integrated security=true";
    string sql_Login = "select * from Users where user=@user AND password=@password";
    protected void Page_Load(object sender, EventArgs e)
    {
        pnlloginMessage.Visible = false;
    }
    private void login(string _user, string _password)
    {
        bool? a = true;
        if (bs.sp_isUserExist(_user, _password, ref a)>0) {
            FormsAuthentication.SetAuthCookie(txtUser.Text.Trim(), true);
            Session["login"] =(txtUser.Text.Trim());
            if (FormsAuthentication.GetRedirectUrl(Session["login"].ToString(), true) != null)
            {
                Response.Redirect(FormsAuthentication.GetRedirectUrl(Session["login"].ToString(), true));
            }
            else {
                Response.Redirect("index.aspx");
            }
           
        }
        else
        {
            pnlloginMessage.Visible = true;
        }
    }


    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (this.IsValid)
        {
            login(
                txtUser.Text.Trim(),
                txtPassword.Text.Trim()
                );
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}